package com.itheima.imclient100.presenter.impl;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.itheima.imclient100.callback.MyEmCallBack;
import com.itheima.imclient100.presenter.ChatPresenter;
import com.itheima.imclient100.utils.ThreadUtils;
import com.itheima.imclient100.view.ChatView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fullcircle on 2017/7/4.
 */

public class ChatPresenterImpl implements ChatPresenter {

    private ChatView chatView;

    public ChatPresenterImpl(ChatView chatView) {
        this.chatView = chatView;
    }

    private List<EMMessage> messages = new ArrayList<>();

    @Override
    public void getHistoryMessage(String username) {
        //获取和某人的会话对象
        EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username);
        if(conversation !=null){
            //获取最近的一条消息
            EMMessage lastMessage = conversation.getLastMessage();
            //获取最近一条消息的消息id
            String msgId = lastMessage.getMsgId();
            //获取所有的消息数量
            int count = conversation.getAllMsgCount();
            //这个方法只返回二十条消息记录
            //List<EMMessage> allMessages = conversation.getAllMessages();
            //第一个参数 消息id 从这条消息开始获取 (不包括这一条消息)
            //第二个参数 要获取的消息数量
            List<EMMessage> emMessages = conversation.loadMoreMsgFromDB(msgId, count);
            //清空保存消息记录的集合
            messages.clear();
            //添加所有的消息
            messages.addAll(emMessages);
            //把最近一条消息添加到集合中
            messages.add(lastMessage);
            chatView.onGetHistoryMessage(messages);
        }else{
            chatView.onGetHistoryMessage(null);
        }
    }

    @Override
    public void sendMessage(String username, String content) {
        //createTxtSendMessage 创建一个等待发送的文本消息
        //参数一消息内容
        //参数二 收消息的用户名
        final EMMessage message = EMMessage.createTxtSendMessage(content, username);
        // 给消息设置消息状态变化的回调
        message.setMessageStatusCallback(new MyEmCallBack() {
            @Override
            public void success() {
                //不管是哪种状态 只要状态变化了 就是通知界面刷新当前的列表
                chatView.updateMessageList();
            }

            @Override
            public void error(int i, String s) {
                chatView.updateMessageList();
            }

            @Override
            public void progress(int i, String s) {
                chatView.updateMessageList();
            }
        });
                messages.add(message);
        chatView.onGetHistoryMessage(messages);
        ThreadUtils.runOnSubThread(new Runnable() {
            @Override
            public void run() {
                //发送消息
                EMClient.getInstance().chatManager().sendMessage(message);
                //把消息放到集合中
//                ThreadUtils.runOnMainThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        chatView.onSendMessage(messages);
//                    }
//                });
            }
        });

    }
}
